package com.yx.demo

import android.graphics.Bitmap
import android.os.Bundle
import android.util.SparseArray
import androidx.databinding.DataBindingUtil
import com.hqw.temp.QDTempModule
import com.hqw.temp.TempModule
import com.hqw.temp.TempModuleCallback
import com.yx.demo.databinding.ActivityTemperatureBinding

class TemperatureActivity : BaseActivity() {

    private lateinit var binding: ActivityTemperatureBinding
    private lateinit var tempModule: TempModule

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this, R.layout.activity_temperature)

        tempModule = QDTempModule()
        tempModule.init(this, null)
    }

    override fun onResume() {
        super.onResume()
        tempModule.start(tempCallBack)
    }

    override fun onPause() {
        super.onPause()
        tempModule.stop()
    }

    override fun onDestroy() {
        super.onDestroy()
        tempModule.onDestroy()
    }


    private val tempCallBack: TempModuleCallback = object: TempModuleCallback{

        /**
         * 模组初始化完成回调
         * @param info 模组硬件版本信息
         */
        override fun onReadInfo(info: String?) {
            
        }


        /**
         * 模组每帧热成像测温数据
         * @param tempArray 测温集合, 摄氏度(℃)
         * @param thermalImaging 热成像图
         */
        override fun onCallback(tempArray: SparseArray<String>?, thermalImaging: Bitmap?) {
            thermalImaging?.let {
                runOnUiThread {
                    binding.tempImage.setImageBitmap(thermalImaging)
                }
            }

            tempArray?.let {
                runOnUiThread {
                    binding.tempText.text = tempArray.get(0)
                }
            }
        }
    }
}